package com.hp.controller;

import com.hp.pojo.Friend;
import com.hp.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cksh
 * @create 2021-06-22 19:11
 */
@RestController
@RequestMapping("/hp")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @PostMapping("/getChatFrames")
    public Map<String, Object> getChatFrames(int userId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.getChatFrames(userId);
    }

    @PostMapping("/getAllNewsByUserId")
    public Map<String, Object> getAllNewsByUserId(int userId) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.getAllNewsByUserId(userId);
    }

    @PostMapping("/updateBlackList")
    public Map<String, Object> updateBlackList(int userId, int chatId, int black) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.updateBlackList(userId, chatId, black);
    }

    @PostMapping("/getFriends")
    public Map<String, Object> getFriends(int userId) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.getFriends(userId);
    }

    @PostMapping("/delFriend")
    public Map<String, Object> delFriend(int userId, int friendId) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.delFriend(userId, friendId);
    }

    @PostMapping("/delRecords")
    public Map<String, Object> delRecords(int userId, int chatId, int source) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.delRecords(userId, chatId, source);
    }

    @PostMapping("/updateNickname")
    public Map<String, Object> updateNickname(int userId, int friendId, String nickname) {
//        if (session.getAttribute("userId") == null) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("msg", "请先登录！");
//            map.put("status", -1);
//            return map;
//        }
        return newsService.updateNickname(userId, friendId, nickname);
    }

    @PostMapping("/addChatFrame")
    public Map<String, Object> addChatFrame(int userId, int chatId, int chatType, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.addChatFrame(userId, chatId, chatType);
    }

    @PostMapping("/delVerification")
    public Map<String, Object> delVerification(int userId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.delVerification(userId);
    }

    @PostMapping("/selectAllByUserId")
    public Map<String, Object> selectAllByUserId(int userId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.selectAllByUserId(userId);
    }

    @PostMapping("/delGroupVerification")
    public Map<String, Object> delGroupVerification(int userId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.delGroupVerification(userId);
    }

    @PostMapping("/selectAllGroupVerificationByUserId")
    public Map<String, Object> selectAllGroupVerificationByUserId(int userId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.selectAllGroupVerificationByUserId(userId);
    }

    @PostMapping("/selectOneFriend")
    public Map<String, Object> selectOneFriend(int userId, int friendId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.selectOneFriend(userId, friendId);
    }

    @PostMapping("/selectOneVerification")
    public Map<String, Object> selectOneVerification(int userId, int fromId, int toId, int verificationStatus, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.selectOneVerification(userId, fromId, toId, verificationStatus);
    }

    @PostMapping("/addFriend")
    public Map<String, Object> addFriend(int userId, int friendId, HttpSession session) {
        if (session.getAttribute("userId") == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg", "请先登录！");
            map.put("status", -1);
            return map;
        }
        return newsService.addFriend(userId, friendId);
    }

}
